package com.wf.smartcomm.server.admin.action;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.opensymphony.xwork2.ActionSupport;
import com.wf.smartcomm.server.bean.User;
import com.wf.smartcomm.server.model.Page;
import com.wf.smartcomm.server.util.StringUtil;

/** 
 * @author 38884306@qq.com  
 * @version 创建时间：2015-5-21 下午3:47:21 
 * 
 */
@Controller(value="adminBaseAction")
@Scope("prototype")
public class BaseAction extends ActionSupport implements ServletRequestAware,ServletResponseAware {

	protected HttpServletRequest request;
	protected HttpServletResponse response;
	protected HttpSession session;
	protected Page page = new Page();;
	protected Gson outGson = new GsonBuilder()
	.excludeFieldsWithoutExposeAnnotation().create();
	protected User currentUser;
	
	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
		session = request.getSession();
		currentUser = (User) session.getAttribute("loginUser");
		String pageSize = request.getParameter("pageSize");
		String pageIndex = request.getParameter("pageIndex");
		if (!StringUtil.isEmpty(pageSize)) {
			page.setPageSize(Integer.parseInt(pageSize));
		}
		if (!StringUtil.isEmpty(pageIndex)) {
			page.setCurrentPage(Integer.parseInt(pageIndex));
		}
	}

	protected void printJsonData(Object json) throws IOException {
		response.setContentType("text/json;charset=UTF-8");
		PrintWriter out = response.getWriter();
		out.print(json);
		out.close();
	}
}
